package com.xs.designs.duty_chain.temp;

import com.xs.designs.duty_chain.pojo.ProductDto;
import com.xs.designs.duty_chain.chain.IHandler;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.List;

public class ProductChainSpringFactory<T,R> {

    private IHandler<T,R> first;

    /**
     * 存放系统中责任链具体处理类
     * @param handlerList
     */
    public  ProductChainSpringFactory(List<IHandler<T,R>> handlerList) {
        Assert.notEmpty(handlerList,"无责任实现bean");

        for (int i = 0; i < handlerList.size()-1; i++) {
            handlerList.get(i).setNextHandler(handlerList.get(i+1));
        }
        first=handlerList.get(0);
    }

    /**
     * 执行具体业务场景中的责任链集合
     */
    public R executeHandle(T productDto) {
       return first.handle(productDto);
    }
}
